package net.huashitong.Indicators.service;

import com.sirdc.modules.sys.service.RedisService;
import com.sirdc.modules.sys.service.SysScheduleJobService;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.Indicators.dao.BA02Dao;
import net.huashitong.Indicators.dto.OverInfoDTO;
import net.huashitong.Indicators.entity.BA02;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;

@Service
public class ManageTimeService {

    private static Logger log  = LoggerFactory.getLogger(ManageTimeService.class);

    @Autowired
    private SysScheduleJobService scheduleJobService;
    @Autowired
    private ScheJobService scheJobService;

    @Autowired
    private BA02Dao dao;

    @Autowired
    private AB02Service ab02Service;

    @Autowired
    private BA02Service ba02Service;

    @Autowired
    private RedisService redisService;

    private OverInfoDTO overInfoDTO;

    @Autowired
    private BA01Service ba01Service;

    @Autowired
    private AB01Service ab01Service;




    private Timer startTimer,secondTimer,overTimer;



    public void  createOrUpdateDate(String bab201, String bab202,String bab203,String bab205) throws ParseException, SQLException {
        //String coursetime  = Until.getCurrentDate();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
       // String years =Until.getHalfYear(coursetime);

      BA02 ab02s  = dao.queryDate(bab205);
        //String ab02s = redisService.get("semesterDate");

        Date startDate =null,secondDate=null,overDate=null;

        startDate =sdf.parse(bab201);
        secondDate=sdf.parse(bab203);
        overDate=sdf.parse(bab202);

        if(ObjectUtils.isBlank(ab02s))
        {
            BA02 dates = new BA02();
            dates.setBAB201(bab201);
            dates.setBAB202(bab202);
            dates.setBAB203(bab203);
            dates.setBAB205(bab205);
            dates.setBAB205(bab205);
            dates.setAAA100("1");

            ab01Service.batchUpdateAb01("10000");

           /* try {

            } catch (SQLException e) {
                log.info("更新账户余额出错");
                return;
            }*/

            ba02Service.save(dates);
           /* redisService.set("choosestatue","0");
            redisService.set("resultCode","0");
            redisService.set("startdate",bab201);
            redisService.set("seconddate",bab203);
            redisService.set("overdate",bab202);*/
        }
        else {
            // BA02 dates  = ab02;
            ab02s.setBAB201(bab201);
            ab02s.setBAB202(bab202);
            ab02s.setBAB203(bab203);
            ab02s.setBAB205(bab205);
            ba02Service.update(ab02s);

        }

        redisService.set("choosestatue","0");
        redisService.set("resultCode","0");
        redisService.set("startdate",   bab201);
        redisService.set("seconddate",  bab203);
        redisService.set("overdate",    bab202);
        redisService.set("semesterDate",bab205);

        overInfoDTO  = new OverInfoDTO();
        overInfoDTO.setBa02Service(ba02Service);
        overInfoDTO.setAb02Service(ab02Service);
        overInfoDTO.setMessage(bab202);
        overInfoDTO.setRedisService(redisService);
        overInfoDTO.setScheduleJobService(scheduleJobService);
        overInfoDTO.setScheJobService(scheJobService);
        overInfoDTO.setBa01Service(ba01Service);

        if(ObjectUtils.isBlank(startTimer))
        {
            startTimer  = new Timer();
            startTimer.schedule(new ItemJobService(redisService,bab201),startDate);
            secondTimer  = new Timer();
            secondTimer.schedule(new SecondProjectJobService(scheJobService,bab203,scheduleJobService),secondDate);
            overTimer = new Timer();
            overTimer.schedule(new OverCourseJobService(overInfoDTO),overDate );

        }else {

            try {
                startTimer.cancel();
                secondTimer.cancel();
                overTimer.cancel();
            }catch (Exception e)
            {
                System.out.println("已经取消了");
            }
            finally {
                startTimer=null;
                secondTimer=null;
                overTimer=null;
            }
            startTimer  = new Timer();
            startTimer.schedule(new ItemJobService(redisService, bab201),startDate);
            secondTimer  = new Timer();
            secondTimer.schedule(new SecondProjectJobService(scheJobService,bab203,scheduleJobService),secondDate);
            overTimer = new Timer();
            overTimer.schedule(new OverCourseJobService(overInfoDTO),overDate );

        }



    }



}
